package com.gmom.tako.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.gmom.tako.model.Rss;
import com.gmom.tako.model.Url;

public class DbAccessor {

	public RSSSQLiteOpenHelper dbhelper;
	public SQLiteDatabase db;

	/**
	 * DB接続
	 * @param context
	 */
	public void Open(Context context) {
		if (dbhelper == null){
			dbhelper = new RSSSQLiteOpenHelper(context);
		}
		if (db == null){
			db = dbhelper.getWritableDatabase();						
		}
	}

	/**
	 * DB切断
	 */
	public void Close() {
		if (db.isOpen()) {
			db.close();
		}
	}

	/**
	 * URL　一件登録
	 * 
	 * @param context
	 * @param rss
	 * @return
	 */
	public long insertRss(Context context, Rss rss) {

		// DB open
		Open(context);

		// DAO
		articleDao arDao = new articleDao(db);

		// INSRET
		long result = arDao.insert(rss);

		// DB close
		Close();

		return result;
	}

	/**
	 * RSS 一括登録
	 * 
	 * @param context
	 * @param rssList
	 * @return long
	 */
	public long insertRssList(Context context, List<Rss> rssList) {

		// DB open
		Open(context);
		
		// DAO
		articleDao arDao = new articleDao(db);

		// INSRET
		int cnt = rssList.size();
		for (int i = 0; i < cnt; i++) {
			Rss rss = rssList.get(i);
			// 重複チェック
			if (arDao.findByUid(rss.getUid()) == null) {
				arDao.insert(rss);
				//Log.v("insertRssList", "insert");
			}
		}

		// DB close
		Close();

		return 0;
	}

	/*
	 * public Rss getRss(int uid) { return new Rss(); }
	 */

	/**
	 * RSS一括取得
	 * 
	 * @param context
	 * @param url_id
	 * @return List<Rss>
	 */
	public List<Rss> getRssList(Context context, int url_id) {

		// DB open
		Open(context);
		
		List<Rss> rssList = new ArrayList<Rss>();
		try {

			articleDao arDao = new articleDao(db);
			rssList = arDao.findByUrlId(url_id);

		} catch (SQLException e) {

			//Log.e("getRssList", e.getMessage());

		} finally {
			Close();
		}
		return rssList;
	}

	/**
	 * URL登録
	 * 
	 * @param context
	 * @param url
	 * @return long
	 */
	public long insertUrl(Context context, Url url) {

		long result = 0;

		// DB open
		Open(context);
		
		// DAO
		urlDao urlDao = new urlDao(db);

		// 重複チェック
		if (urlDao.findByUrl(url.getUrl()) == null) {
			// INSRET
			result = urlDao.insert(url);
			//Log.v("insertUrl", "insert");
		}

		// DB close
		Close();

		return result;

	}

	/*
	 * public Url getUrl(int url_id) { Url url = new Url(); return url; }
	 */

	/**
	 * URLをListで一括取得
	 * 
	 * @param context
	 * @return
	 */
	public List<Url> getUrlList(Context context) {

		// DB open
		Open(context);
		
		List<Url> urlList = new ArrayList<Url>();
		try {

			urlDao urlDao = new urlDao(db);
			urlList = urlDao.findAll();

		} catch (SQLException e) {

			//Log.e("getUrlList", e.getMessage());

		} finally {
			Close();
		}
		return urlList;
	}

}
